//
//  AboutView.m
//  atlas
//
//  Created by 你好 on 17/2/14.
//  Copyright © 2017年 xyxNav. All rights reserved.
//

#import "AboutView.h"

@implementation AboutView



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self)
    {
        self.showsVerticalScrollIndicator=NO;
        self.showsHorizontalScrollIndicator=NO;
        self.scrollEnabled=NO;
    }
    return self;
}




-(void)configUI
{
    self.backView=[UIView new];
    [self addSubview:self.backView];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.right.equalTo(self).with.insets(UIEdgeInsetsZero);
        make.width.equalTo(self);
    }];
    
    self.titleLabel=[UILabel new];
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    self.titleLabel.font=[UIFont systemFontOfSize:22];
    [self.backView addSubview:self.titleLabel];
    
    self.titleTextView=[UITextView new];
    self.titleTextView.textContainer.lineFragmentPadding=0.0f;
    self.titleTextView.textContainerInset=UIEdgeInsetsZero;
    self.titleTextView.editable=NO;
    self.titleTextView.scrollEnabled=NO;
    self.titleTextView.font=[UIFont fontWithName:@"STHeitiSC-Light" size:14];
    [self.backView addSubview:self.titleTextView];
    
    self.businessLabel=[UILabel new];
    self.businessLabel.textAlignment=NSTextAlignmentLeft;
    [self.backView addSubview:self.businessLabel];
    
    self.businessTextView=[UITextView new];
    self.businessTextView.editable=NO;
    self.businessTextView.textContainer.lineFragmentPadding=0.0f;
    self.businessTextView.textContainerInset=UIEdgeInsetsZero;
    self.businessTextView.scrollEnabled=NO;
    self.businessTextView.font=[UIFont fontWithName:@"STHeitiSC-Light" size:14];
    [self.backView addSubview:self.businessTextView];
    
    UIImage *addressImage=[UIImage imageNamed:@"address_icon"];
    UIImage *phoneImage=[UIImage imageNamed:@"phone_icon_black"];
    
    self.addressImageView=[UIImageView new];
    self.addressImageView.image=addressImage;
    [self.backView addSubview:self.addressImageView];
    
    self.addressLabel=[UILabel new];
    self.addressLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:12];
    [self.backView addSubview:self.addressLabel];
    
    self.phoneImageView=[UIImageView new];
    self.phoneImageView.image=phoneImage;
    [self.backView addSubview:self.phoneImageView];
    
    self.phoneLabel=[UILabel new];
    self.phoneLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:12];
    [self.backView addSubview:self.phoneLabel];
    
    self.lineView=[UIView new];
    self.lineView.backgroundColor=RGBACOLOR(1, 1, 1, 0.3);
    [self.backView addSubview:self.lineView];
    
}

-(void)configData:(About *)about
{
    UIImage *addressImage=[UIImage imageNamed:@"address_icon"];
    UIImage *phoneImage=[UIImage imageNamed:@"phone_icon_black"];
    
    double titleTextHeight=0.0f;
    double businessTextHeight=0.0f;
    
//    NSString *testStr=@"说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低说到iOS自动布局，有很多的解决办法。有的人使用xib/storyboard自动布局，也有人使用frame来适配。对于前者，笔者并不喜欢，也不支持。对于后者，更是麻烦，到处计算高度、宽度等，千万大量代码的冗余，对维护和开发的效率都很低";
    
    if (about.introduce.length>0)
    {
        titleTextHeight=[self getHeightOfContent:about.introduce];
    }
    
    if (about.business.length>0)
    {
        businessTextHeight=[self getHeightOfContent:about.business];
    }

    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self.backView);
        make.height.equalTo(@60);
    }];
    
    [self.titleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(30);
        make.right.equalTo(self.backView).offset(-30);
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.height.equalTo(@(titleTextHeight));
    }];
    
    [self.businessLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(30);
        make.right.equalTo(self.backView).offset(-30);
        make.top.equalTo(self.titleTextView.mas_bottom);
        make.height.equalTo(@50);
        
    }];
    
    [self.businessTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.backView).offset(30);
        make.right.equalTo(self.backView).offset(-30);
        make.top.equalTo(self.businessLabel.mas_bottom);
        make.height.equalTo(@(businessTextHeight));
    }];
    
    [self.titleTextView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.height.equalTo(@(titleTextHeight));
    }];

    [self.businessTextView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(30);
        make.right.equalTo(self).offset(-30);
        make.top.equalTo(self.businessLabel.mas_bottom);
        make.height.equalTo(@(businessTextHeight));
    }];
    
    
    if ((titleTextHeight+businessTextHeight+168.5)>self.bounds.size.height)
    {
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(15);
            make.right.equalTo(self.backView).offset(-15);
            make.height.equalTo(@0.5);
            make.top.equalTo(self.businessTextView.mas_bottom).offset(30);
        }];
        
        [self.addressImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.width.equalTo(@(addressImage.size.width));
            make.height.equalTo(@(addressImage.size.height));
            make.top.equalTo(self.lineView).offset(13);
        }];
        
        [self.addressLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.addressImageView.mas_right).offset(5);
            make.right.equalTo(self.backView);
            make.top.equalTo(self.lineView.mas_bottom).offset(10);
            make.height.equalTo(@18);
        }];
        
        [self.phoneImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.width.equalTo(@(phoneImage.size.width));
            make.height.equalTo(@(phoneImage.size.height));
            make.top.equalTo(self.addressLabel.mas_bottom).offset(8);
        }];
        
        [self.phoneLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.phoneImageView.mas_right).offset(5);
            make.right.equalTo(self.backView);
            make.top.equalTo(self.addressLabel.mas_bottom);
            make.height.equalTo(@27);
        }];
        
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self).with.insets(UIEdgeInsetsZero);
            make.width.equalTo(self);
            make.bottom.equalTo(self.phoneLabel.mas_bottom).offset(10);
        }];
    }
    else
    {
        [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.bottom.right.equalTo(self).with.insets(UIEdgeInsetsZero);
            make.width.height.equalTo(self);
        }];
        
        [self.phoneImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.bottom.equalTo(self.backView).offset(-10);
            make.width.equalTo(@(phoneImage.size.width));
            make.height.equalTo(@(phoneImage.size.height));
        }];
        
        [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.phoneImageView.mas_right).offset(5);
            make.right.bottom.equalTo(self.backView);
            make.height.equalTo(@30);
        }];
        
        [self.addressImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(30);
            make.bottom.equalTo(self.phoneLabel.mas_top).offset(-4);
            make.width.equalTo(@(addressImage.size.width));
            make.height.equalTo(@(addressImage.size.height));
            
        }];
        
        [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.addressImageView.mas_right).offset(5);
            make.right.equalTo(self.backView);
            make.bottom.equalTo(self.phoneLabel.mas_top);
            make.height.equalTo(@18);
            
        }];
        
        [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.backView).offset(15);
            make.right.equalTo(self.backView).offset(-15);
            make.bottom.equalTo(self.addressLabel.mas_top).offset(-10);
            make.height.equalTo(@0.5);
        }];
    }

    NSMutableParagraphStyle *paraStyle=[[NSMutableParagraphStyle alloc]init];
    paraStyle.lineSpacing=8.0f;

    NSDictionary *attrButeDict=@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont fontWithName:@"STHeitiSC-Light" size:14]};
    
    NSMutableAttributedString *attrIntroduceStr=nil;

    if (about.introduce.length>0)
    {
        attrIntroduceStr=[[NSMutableAttributedString alloc]initWithString:about.introduce];
        [attrIntroduceStr addAttributes:attrButeDict range:NSMakeRange(0, about.introduce.length)];
        self.titleTextView.attributedText=attrIntroduceStr;
    }

    NSMutableAttributedString *attrBusinessStr=nil;
    
    if (about.business.length>0)
    {
        attrBusinessStr=[[NSMutableAttributedString alloc]initWithString:about.business];
        [attrBusinessStr addAttributes:attrButeDict range:NSMakeRange(0, about.business.length)];
        self.businessTextView.attributedText=attrBusinessStr;
    }

    self.titleLabel.text=about.company;

    self.businessLabel.text=@"主营业务";

    NSString *addressStr=[NSString stringWithFormat:@"地址：%@",about.address];
    NSString *phoneStr=[NSString stringWithFormat:@"电话：%@     QQ：%@",about.tel,about.qq];
    self.addressLabel.text=addressStr;
    self.phoneLabel.text=phoneStr;
}


-(CGFloat)getHeightOfContent:(NSString *)str
{
    NSMutableParagraphStyle *paraStyle=[[NSMutableParagraphStyle alloc]init];
    paraStyle.lineSpacing=8.0f;
    
    NSDictionary *dict=@{NSParagraphStyleAttributeName:paraStyle,NSFontAttributeName:[UIFont fontWithName:@"STHeitiSC-Light" size:14]};
    
    CGRect rect=[str boundingRectWithSize:CGSizeMake(self.bounds.size.width-60, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:dict context:nil];
    
    return rect.size.height;
}



@end
